package MyApp;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;

import com.example.myapplication.R;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MemoActivity extends AppCompatActivity implements MemoAdapter.OnDeleteClickListener {
    private RecyclerView memoRecyclerView;
    private MemoAdapter memoAdapter;
    private List<Memo> memoList;
    private static final String MEMOS_PREFS = "memos_prefs";
    private static final String MEMO_KEY = "memo_";
    private static final String TIMESTAMP_KEY = "timestamp_";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_memo);

        memoList = new ArrayList<>();
        memoRecyclerView = findViewById(R.id.memoRecyclerView);
        memoAdapter = new MemoAdapter(memoList, this);
        memoRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        memoRecyclerView.setAdapter(memoAdapter);

        loadMemos();

        // 添加备忘录按钮的点击事件
        findViewById(R.id.addMemoButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addMemo();
            }
        });
    }

    private void loadMemos() {
        SharedPreferences sharedPreferences = getSharedPreferences(MEMOS_PREFS, MODE_PRIVATE);
        int size = sharedPreferences.getInt("size", 0);
        for (int i = 0; i < size; i++) {
            String content = sharedPreferences.getString(MEMO_KEY + i, "");
            String timestamp = sharedPreferences.getString(TIMESTAMP_KEY + i, "");
            memoList.add(new Memo(content, timestamp));
        }
        memoAdapter.notifyDataSetChanged();
    }

    private void saveMemos() {
        SharedPreferences sharedPreferences = getSharedPreferences(MEMOS_PREFS, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt("size", memoList.size());
        for (int i = 0; i < memoList.size(); i++) {
            Memo memo = memoList.get(i);
            editor.putString(MEMO_KEY + i, memo.getContent());
            editor.putString(TIMESTAMP_KEY + i, memo.getTimestamp());
        }
        editor.apply();
    }

    @Override
    public void onDeleteClick(int position) {
        // 从列表中移除备忘录项
        memoList.remove(position);
        // 保存备忘录数据
        saveMemos();
        // 通知适配器数据已更改
        memoAdapter.notifyItemRemoved(position);
    }

    private void addMemo() {
        AlertDialog.Builder builder = new AlertDialog.Builder(MemoActivity.this);
        builder.setTitle("新建备忘录");
        final EditText input = new EditText(MemoActivity.this);
        input.setInputType(InputType.TYPE_CLASS_TEXT);
        builder.setView(input);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String content = input.getText().toString();
                if (!content.isEmpty()) {
                    Memo memo = new Memo(content, DateFormat.getDateTimeInstance().format(new Date()));
                    memoList.add(memo);
                    memoAdapter.notifyItemInserted(memoList.size() - 1);
                    saveMemos(); // 保存备忘录数据
                }
            }
        });
        builder.setNegativeButton("取消", null);
        builder.show();
    }

    @Override
    protected void onPause() {
        super.onPause();
        saveMemos(); // 应用暂停时保存备忘录数据
    }
}